package com.huangjue.backend.controller;

import com.huangjue.backend.entity.News;
import com.huangjue.backend.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/news")
@CrossOrigin(origins = "*")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 获取最新的4条已发布新闻 - 首页展示
     */
    @GetMapping("/latest")
    public ResponseEntity<Map<String, Object>> getLatestNews() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            List<News> news = newsService.getLatestPublishedNews();
            response.put("success", true);
            response.put("message", "获取最新新闻成功");
            response.put("data", news);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取最新新闻失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 获取所有已发布的新闻 - 新闻列表页
     */
    @GetMapping("/all")
    public ResponseEntity<Map<String, Object>> getAllPublishedNews() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            List<News> news = newsService.getAllPublishedNews();
            response.put("success", true);
            response.put("message", "获取新闻列表成功");
            response.put("data", news);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取新闻列表失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 根据ID获取新闻详情
     */
    @GetMapping("/detail/{id}")
    public ResponseEntity<Map<String, Object>> getNewsDetail(@PathVariable Integer id) {
        Map<String, Object> response = new HashMap<>();
        
        if (id == null || id <= 0) {
            response.put("success", false);
            response.put("message", "新闻ID无效");
            return ResponseEntity.badRequest().body(response);
        }
        
        try {
            News news = newsService.getNewsById(id);
            if (news == null) {
                response.put("success", false);
                response.put("message", "新闻不存在");
                return ResponseEntity.notFound().build();
            }
            
            // 更新浏览次数
            newsService.updateNewsViewCount(id);
            
            response.put("success", true);
            response.put("message", "获取新闻详情成功");
            response.put("data", news);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取新闻详情失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
} 